package org.ala.tiktools.generator.mybatis.creator.shard;

import freemarker.template.TemplateException;
import org.ala.tiktools.generator.mybatis.config.info.Part;
import org.ala.tiktools.generator.mybatis.creator.AbstractCreator;
import org.ala.tiktools.generator.mybatis.creator.dao.DaoInfo;
import org.ala.tiktools.generator.mybatis.creator.dao_shard.DaoShardInfo;
import org.ala.tiktools.generator.mybatis.creator.entity.EntityInfo;
import org.ala.tiktools.generator.mybatis.creator.query.QueryInfo;
import org.ala.tiktools.generator.mybatis.dbinfo.TableInfo;
import org.ala.tiktools.generator.mybatis.tools.Tools;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ala
 * @date 2025-02-25 16:27
 */
public class ShardRouterCreator extends AbstractCreator {

    public ShardRouterInfo[] create(Part part, TableInfo t, EntityInfo e, QueryInfo q) throws TemplateException, IOException {
        return new ShardRouterInfo[]{
                createDO(part, t, e, q),
                createDOList(part, t, e, q),
                createQuery(part, t, e, q),
                createMu(part, t, e, q)
        };
    }


    /**
     *  生成DO router
     */
    protected ShardRouterInfo createDO(Part part, TableInfo t, EntityInfo e, QueryInfo q) throws TemplateException, IOException {
        String generics = e.getPkg() + "." + e.getClassName();
        ShardRouterInfo r = ShardRouterInfo.build(part, t, e, generics, false);

        Map<String, Object> dataMeta = new HashMap<>();
        dataMeta.put("r", r);

        String out = r.getPkg();
        out = this.getOutRootPath() + out.replaceAll("\\.", "/");
        Tools.existsAndMakeDirs(out);
        out = out + "/" + r.getClassName() + ".java";

        output("ShardRouter.ftl", dataMeta, out);

        System.out.println("生成DORouter类. path:" + out);
        return r;
    }
    /**
     *  生成DOList router
     */
    protected ShardRouterInfo createDOList(Part part, TableInfo t, EntityInfo e, QueryInfo q) throws TemplateException, IOException {
        String generics = e.getPkg() + "." + e.getClassName();
        ShardRouterInfo r = ShardRouterInfo.build(part, t, e, generics, true);

        Map<String, Object> dataMeta = new HashMap<>();
        dataMeta.put("r", r);

        String out = r.getPkg();
        out = this.getOutRootPath() + out.replaceAll("\\.", "/");
        Tools.existsAndMakeDirs(out);
        out = out + "/" + r.getClassName() + ".java";

        output("ShardRouter.ftl", dataMeta, out);

        System.out.println("生成DOListRouter类. path:" + out);
        return r;
    }
    /**
     *  生成Query router
     */
    protected ShardRouterInfo createQuery(Part part, TableInfo t, EntityInfo e, QueryInfo q) throws TemplateException, IOException {
        String generics = q.getPkg() + "." + q.getClassName();
        ShardRouterInfo r = ShardRouterInfo.build(part, t, e, generics, false);

        Map<String, Object> dataMeta = new HashMap<>();
        dataMeta.put("r", r);

        String out = r.getPkg();
        out = this.getOutRootPath() + out.replaceAll("\\.", "/");
        Tools.existsAndMakeDirs(out);
        out = out + "/" + r.getClassName() + ".java";

        output("ShardRouter.ftl", dataMeta, out);

        System.out.println("生成QueryRouter类. path:" + out);
        return r;
    }
    /**
     *  生成MutileUpdate router
     */
    protected ShardRouterInfo createMu(Part part, TableInfo t, EntityInfo e, QueryInfo q) throws TemplateException, IOException {
//        MultipleUpdate
        String generics = part.getOut().getDao().getPkg() + "." + Tools.upHump(t.getName()) + part.getOut().getDao().getSuffix() + ".MultipleUpdate";
        ShardRouterInfo r = ShardRouterInfo.build(part, t, e, generics, true);
        r.className = Tools.upHump(t.getName()) + r.className;

        Map<String, Object> dataMeta = new HashMap<>();
        dataMeta.put("r", r);

        String out = r.getPkg();
        out = this.getOutRootPath() + out.replaceAll("\\.", "/");
        Tools.existsAndMakeDirs(out);
        out = out + "/" + r.getClassName() + ".java";

        output("ShardRouter.ftl", dataMeta, out);

        System.out.println("生成MultipleUpdateRouter类. path:" + out);
        return r;
    }
}
